//
//  YZLDiscussViewController.m
//  Car
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLDiscussViewController.h"
#import "YZLDiscussTitleView.h"
#import "YZLSelectCollectionViewCell.h"
#import "YZLHotTieCollectionViewCell.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface YZLDiscussViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) YZLDiscussTitleView *bar;
@property (nonatomic, strong) UIView *line;

@property (nonatomic, strong) UICollectionView *collection;

@end

@implementation YZLDiscussViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}


- (void)creatBar {
    self.bar = [[YZLDiscussTitleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60*HEIGHT/667)];
    [self.view addSubview:self.bar];
    self.line = [[UIView alloc]initWithFrame:CGRectMake(0, 57.5*HEIGHT/667, self.view.frame.size.width/4, 2.5*HEIGHT/667)];
    self.line.backgroundColor = [UIColor cyanColor];
    [self.bar addSubview:self.line];
}

- (void)creatCollection {
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height-60*HEIGHT/667);
    flow.minimumLineSpacing = 0;
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60*HEIGHT/667, WIDTH, self.view.frame.size.height-60*HEIGHT/667) collectionViewLayout:flow];
    [self.view addSubview:self.collection];

    self.collection.delegate =self;
    self.collection.dataSource = self;
    [self.collection registerClass:[YZLSelectCollectionViewCell class] forCellWithReuseIdentifier:@"select"];
    [self.collection registerClass:[YZLHotTieCollectionViewCell class] forCellWithReuseIdentifier:@"hot"];
    self.collection.pagingEnabled = YES;
    //留边
//    self.collection.bounces = NO;
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.showsHorizontalScrollIndicator = NO;
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.item) {
        YZLSelectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"select" forIndexPath:indexPath];
        return cell;
    } else {
        YZLHotTieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hot" forIndexPath:indexPath];
        return cell;
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[touches allObjects] lastObject];
    if (self.bar.label1 == touch.view) {
        self.bar.label1.textColor = [UIColor cyanColor];
        self.bar.label2.textColor = [UIColor blackColor];
        [self.collection setContentOffset:CGPointMake(0, 0) animated:YES];
    } else if (self.bar.label2 == touch.view) {
        self.bar.label2.textColor = [UIColor cyanColor];
        self.bar.label1.textColor = [UIColor blackColor];
        [self.collection setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
    }
}

//滑动时调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.line.frame = CGRectMake((self.view.frame.size.width/4)*(scrollView.contentOffset.x/WIDTH), 57.5*HEIGHT/667, self.view.frame.size.width/4, 2.5*HEIGHT/667);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        self.bar.label1.textColor = [UIColor cyanColor];
        self.bar.label2.textColor = [UIColor blackColor];
    } else if (scrollView.contentOffset.x == WIDTH) {
        self.bar.label2.textColor = [UIColor cyanColor];
        self.bar.label1.textColor = [UIColor blackColor];
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatCollection];
    [self creatBar];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
